import commonConstants from "@/common/commonConstants";
import MonitorManager from "./MonitorManager";
import { Cartesian3, ScreenSpaceEventHandler, ScreenSpaceEventType, defined } from "cesium";
import commonMethod from "@/common/commonMethod";
import JBEntity from "../marker/compositeentity/JBEntity";
import Marker from "../marker/Marker";
import RightMenu from "@/common/rightMenu";

class JBMonitor{
 
    constructor(viewer){
       
        this.curViewer = viewer;
        this.curScene = this.curViewer.scene;
        this.curCanvas = this.curScene.canvas;

        this.isActive = false;
        this.isEdit = false;
        this.isOneTargger = false;

        this.monitorKey = commonConstants.monitorKey.JB;
        
        this.pickEntity = undefined;
        this.position = undefined;
        this.drawHandler = undefined;
        this.finishCallback = undefined;
        this.params = undefined;
        this.monitorManager = undefined;

        this.rightmenu = undefined;
    }

    /**
     * 
     * @param {MonitorManager} manager 
     */
    setManager(manager){
        const _this = this;
        _this.monitorManager = manager;
    }

    /**
     * 这是设置标绘在标绘时的其他参数
     * @param {*} params 
     */
    setParams(params){
        const _this = this;
        _this.params = params;
    }

    /**
     * @param {Boolean} bool 
     */
    setActive(bool){
        const _this = this;
        _this.isActive = bool;
    }

    /**
     * 设置完成军标标绘后的回调函数
     * @param {Function} callbackFunction 
     */
    setFinishCallback(callbackFunction){
        let _this = this;
        _this.finishCallback = callbackFunction;
    }

    deploy(){
        let _this = this;

        if (!_this.drawHandler) {
            _this.drawHandler = new ScreenSpaceEventHandler(_this.curCanvas);
           
            _this.drawHandler.setInputAction( event => {

                if (!_this.isActive) {
                    return;
                }
 
                let p = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!p) {
                    return;
                }

                _this.destoryRightMenu();
                const cartesian = _this.curScene.globe.pick(p.ray, _this.curScene);

                if (!defined(cartesian)) {
                    return;
                }

                if (_this.isEdit) {
                   //初次触发不能结束
                    if (_this.isOneTargger){
                        _this.isOneTargger = false;
                        return;
                    }
                    //再次进入，表示修改结束
                    _this.isEdit = false;
                
                    //关闭修改
                    _this.monitorManager.deployDefaultMonitor();
                    return;
                }
                const marker = _this.createJbMarker(cartesian);

                if (_this.finishCallback){
                    _this.finishCallback(marker);
                }
            }, ScreenSpaceEventType.LEFT_CLICK);

            _this.drawHandler.setInputAction( event =>{

                if (!_this.isActive) {
                    return;
                }

                _this.destoryRightMenu();
                const judgeClickPosition = commonMethod.judgeClickPosition(event.endPosition,_this.curViewer);
                if (!judgeClickPosition) {
                    return;
                }
                const cartesian = _this.curScene.globe.pick(judgeClickPosition.ray, _this.curScene);

                if (!defined(cartesian)) {
                    return;
                }
                if (_this.isEdit) {
                    //通过控制点，修改标绘的点的位置
                    //_this.pickEntity.movePositon(cartesian);
                    //移动时，如果有连线，也得一起动
                    const markerManager = _this.monitorManager.getMarkerManager();
                    const marker = markerManager.getMarkerById(_this.pickEntity.id);
                    marker.updatePositions(cartesian);
                }
            }, ScreenSpaceEventType.MOUSE_MOVE);

            _this.drawHandler.setInputAction(event => {

                if (!_this.isActive) {
                    return;
                }

                let judgeClickPosition = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!judgeClickPosition) {
                    return;
                }

               //表示直接结束标绘
                _this.position =null;
                _this.pickEntity = null;
                _this.monitorManager.deployDefaultMonitor();
            }, ScreenSpaceEventType.RIGHT_CLICK);
        }
    }

    /**
     * 创建标绘
     * @param {Cartesian3} position 
     * @returns 
     */
    createJbMarker(position){
        const _this = this;
        //生成的军标类型不同
        let jbEntity =new JBEntity(_this.curViewer);
        jbEntity.setParams(_this.params);
        jbEntity.setMonitorKey(_this.monitorKey);
        jbEntity.setEntityType(commonConstants.entityType.marker);
        jbEntity.createJb(position);

        _this.monitorManager.getDefaultMonitor().putEntity(jbEntity);

        return new Marker({
            markerId:jbEntity.getEntityId(),
            isCompositeEntity:true,
            markerName:_this.params.markerName,
            entity:jbEntity,
            position:position,
            markerCode:_this.params.markerCode
        });
    }

    destoryRightMenu(){
        let _this = this;
        if (_this.rightmenu){
            _this.rightmenu.destroy();
            _this.rightmenu = null;
            _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
        }
    }

    showRightMenuEntity(e,entity){
        let _this = this;
        _this.destoryRightMenu();

        const rightMenuOpts = [];
        //属性面板的显示问题，这是鼠标右键开启属性面板的效果
        // let scenarioConfiguration = _this.defaultMonitor.getMonitorManager().getScenarioConfiguration();
        // let rightMenuOpts =[];
        // if (scenarioConfiguration.getConfiguration("attributePanelable")) {
        //     rightMenuOpts.push({
        //         itemName: "属性",
        //         icon: "layui-icon layui-icon-edit",
        //         callback: function () {
        //             scenario_deduce.getAttributePanelUtils().showAttributePanel(entity.id);
        //         }
        //     })
        // }
        rightMenuOpts.push({
            itemName: "删除",
            icon: "layui-icon layui-icon-delete",
            callback: function () {
                //表示删除点标标绘
                const markerManager = _this.monitorManager.getMarkerManager();
                //删除点标之前，先删除关联的线，并在相互关联的节点中删除关联信息
                const markerById = markerManager.getMarkerById(entity.getEntityId());
                if (markerById){
                    //最后删除点标
                    markerManager.removeMarker(markerById);
                }
                //关闭标绘
                _this.monitorManager.deployDefaultMonitor();

            }
        });
        _this.rightmenu = new RightMenu(rightMenuOpts,_this.curViewer);
        _this.rightmenu.show(e,true);
        _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
    }

     /**
     * 处理默认监听带来的事件
     * @param {ScreenSpaceEventType} clickType
     * @param {*} event
     * @param {*} pickEntity   
     */
     handerEvent(event,pickEntity,clickType) {
        const _this = this;
        _this.pickEntity = pickEntity;
        if (clickType === ScreenSpaceEventType.RIGHT_CLICK){
            //右键菜单
            _this.showRightMenuEntity(event,pickEntity);
        }else if (clickType === ScreenSpaceEventType.LEFT_CLICK){
            _this.isEdit = true;
            _this.isActive = true;
            //只为了触发时间和监听对象的转变，不能直接就进入结束状态
            _this.isOneTargger = true;
        }
    }
    reMarker(position,callbackFunction){
        let _this = this;
        const marker = _this.createJbMarker(position);
        if (callbackFunction){
            callbackFunction(marker);
        }
    }
};

export default JBMonitor;